<?php
// +----------------------------------------------------------------------
// | Iecms [ Make the most exquisite CMS ]
// +----------------------------------------------------------------------
// | Copyright (c) 2009~2019 http://www.iemoo.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: iemoo <i@iemoo.com>
// +----------------------------------------------------------------------
// | DateTime: 2019/11/19 11:08
// +----------------------------------------------------------------------

/**
 * 无限分类-栏目
 * @param $cate
 * @param string $lefthtml
 * @param int $pid
 * @param int $lvl
 * @return array
 */
function tree_cate($cate, $lefthtml = '¦— ', $pid = 0, $lvl = 0)
{
    $arr = array();
    foreach ($cate as $v) {
        if ($v['pid'] == $pid) {
            $v['lvl'] = $lvl + 1;
            $v['lefthtml'] = str_repeat($lefthtml, $lvl);
            $v['title'] = $v['lefthtml'] . $v['title'];
            $arr[] = $v;
            $arr = array_merge($arr, tree_cate($cate, $lefthtml, $v['id'], $lvl + 1));
        }
    }
    return $arr;
}


/**
 * 数组转树型
 * @param array $list 要转换的结果集
 * @param string $pid parent标记字段
 * @param string $level level标记字段
 */
function list_to_tree($list, $pk='id', $pid = 'pid', $child = 'children', $root = 0) {
    //创建Tree
    $tree = array();
 
    if (is_array($list)) {
        //创建基于主键的数组引用
        $refer = array();
 
        foreach ($list as $key => $data) {
            $refer[$data[$pk]] = &$list[$key];
        }
 
        foreach ($list as $key => $data) {
            //判断是否存在parent
            $parantId = $data[$pid];
 
            if ($root == $parantId) {
                $tree[] = &$list[$key];
            } else {
                if (isset($refer[$parantId])) {
                    $parent = &$refer[$parantId];
                    $parent[$child][] = &$list[$key];
                }
            }
        }
    }
 
    return $tree;
}

/**
 * 从栏目树中，排除自己及子分类
 * @param $cate
 * @param string $lefthtml
 * @param int $pid
 * @param int $lvl
 * @return array
 */
function category_remove_son($cate_tree, $pid = 0)
{

    foreach ($cate_tree as $k => $v) {
        if ($v['pid'] == $pid) {
			$pid = $v['id'];
			unset($cate_tree[$k]);
            category_remove_son($cate_tree, $pid);
        }
    }

    return $cate_tree;
}


/**
 * 格式化系统设置表中的setup数据
 * @param $system
 * @return mixed
 */
function system_item($system)
{
    foreach ($system as $kel => $val) {
        if (in_array($system[$kel]['type'],['select','radio','checkbox'])  ) {

			$system[$kel]['options'] = explode("\n", $system[$kel]['options']);
			foreach ($system[$kel]['options'] as $k => $v) {
				$system[$kel]['options'][$k] = trim_array_element(explode("|", $system[$kel]['options'][$k]));

			}
 
        }
    }
    return $system;
}

/**
 * 过滤数组元素前后空格 (支持多维数组)
 * @param $array 要过滤的数组
 * @return array|string
 */
function trim_array_element($array)
{
    if (!is_array($array)) return trim($array);
    return array_map('trim_array_element', $array);
}




/**
 * 获取文件目录列表
 * @param string $pathname 路径
 * @param integer $fileFlag 文件列表 0所有文件列表,1只读文件夹,2是只读文件(不包含文件夹)
 * @param string $pathname 路径
 * @return array
 */
function get_file_folder_List($pathname, $fileFlag = 0, $pattern = '*')
{

    $fileArray = array();
    $pathname = rtrim($pathname, '/') . '/';	
    $list = glob($pathname . $pattern);
    foreach ($list as $i => $file) {
        switch ($fileFlag) {
            case 0:
                $fileArray[] = basename($file);
                break;
            case 1:
                if (is_dir($file)) {
                    $fileArray[] = basename($file);
                }
                break;

            case 2:
                if (is_file($file)) {
                    $fileArray[] = basename($file);
                }
                break;

            default:
                break;
        }
    }

    if (empty($fileArray)) $fileArray = NULL;
    return $fileArray;
}

/**
 * 获取所有模版
 * @return mixed
 */
function get_template()
{
	$view_path = config('view.view_dir_name');

    $tpl['list'] = get_file_folder_List('./../'.$view_path.'/index', 2, 'list_*');
    $tpl['show'] = get_file_folder_List('./../'.$view_path.'/index', 2, 'show_*');
	$tpl['page'] = get_file_folder_List('./../'.$view_path.'/index', 2, 'page_*');
		

    return $tpl;
}

